package encrypt

import (
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha1"
	"encoding/hex"
	"hash"
)

func EncryptByMd5(beforeStr string) string {
	h := md5.New()
	h.Write([]byte(beforeStr))
	cipherStr := h.Sum(nil)
	return hex.EncodeToString(cipherStr)
}

func EncryptBySha1(beforeStr string) string {
	h := sha1.New()
	h.Write([]byte(beforeStr))
	cipherStr := h.Sum(nil)
	return hex.EncodeToString(cipherStr)
}

func EncryptByHMac(encryptType func() hash.Hash, beforeStr string, key string) string {
	//HMac, use sha1
	mac := hmac.New(encryptType, []byte(key))
	mac.Write([]byte(beforeStr))
	cipherStr := mac.Sum(nil)
	return hex.EncodeToString(cipherStr)
}
